package com.yami.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.dao.SubmitPersonMapper;
import com.yami.shop.bean.model.SubmitPerson;
import com.yami.shop.service.SubmitPersonService;
import lombok.AllArgsConstructor;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xch
 * @since 2021-07-01
 */
@Service
@AllArgsConstructor
public class SubmitPersonServiceImpl extends ServiceImpl<SubmitPersonMapper, SubmitPerson> implements SubmitPersonService {

    private final SubmitPersonMapper submitPersonMapper;


    @Override
    public SubmitPerson loadSubmitPersonByMobile(String mobile) {

        SubmitPerson submitPerson = submitPersonMapper.selectOne(new LambdaQueryWrapper<SubmitPerson>().eq(SubmitPerson::getMobile,mobile));
        if (Objects.nonNull(submitPerson)){
            return submitPerson;
        }
        // 当前手机号是否为提报人
        int num = submitPersonMapper.selectCollaborativePurchaseNum(mobile);

        if (num == 0){
            // 提报人不存在或该提报人没有可提交商品的协同采购单
            return null;
        }
        // 注册提报人
        submitPerson = new SubmitPerson();
        submitPerson.setMobile(mobile);
        submitPersonMapper.insert(submitPerson);
        return submitPerson;
    }
}
